//
//  CartoonRecommendViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "CartoonRecommendViewController.h"
#import "LunBoTu.h"
//#import "Recommend.h"
#import "Recommend1.h"
#import "RecommendCollectionViewCell.h"
#import "RecommendTableViewCell.h"
#import "PrefixHeader.pch"
#import "BaseModel.h"
#import "RMZQTableViewCell.h"
#import "QDTJTableViewCell.h"
#import "JRTJTableViewCell.h"
#import "FourTableViewCell.h"
#import "TMMRViewController.h"
#import "LBTViewController.h"
#import "MoreViewController.h"
#import "CartoonDetailsViewController.h"
#import "SearchViewController.h"
@interface CartoonRecommendViewController ()<UITableViewDataSource,UITableViewDelegate,LunBoTuDelegate,RMZQCellDelegate,FourCellDelegate,QDTJCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *lightComicArr;
//@property (nonatomic,strong)NSMutableArray *arr;
@property (nonatomic,strong)NSMutableArray *bannerArr;
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic,strong)NSMutableDictionary *dictionary;
@property (nonatomic,strong)NSMutableArray *mangeArr;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)NSMutableArray *comiclist1;
@property (nonatomic,strong)NSMutableArray *comiclist2;
@property (nonatomic,strong)NSMutableArray *comiclist3;
@property (nonatomic,strong)NSMutableArray *comiclist4;
@property (nonatomic,strong)NSMutableArray *comiclist5;
@property (nonatomic,strong)NSMutableArray *comiclist6;
@property (nonatomic,strong)NSMutableArray *comiclist7;
@property (nonatomic,strong)NSMutableArray *comiclist8;
@property (nonatomic,strong)NSMutableArray *comiclist9;
@property (nonatomic,strong)NSMutableArray *limitFreeArr;
@property (nonatomic,strong)NSMutableArray *guessArr;
@property (nonatomic,strong)NSMutableDictionary *bannerDic;
@property (nonatomic,strong)NSMutableDictionary *idDic;
@property (nonatomic,strong)NSMutableDictionary *keyArr;

@end

@implementation CartoonRecommendViewController

-(void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIBarButtonItem *button1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(barButtonAction1:)];
    
    self.navigationItem.rightBarButtonItem = button1;
    
    
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SelfViewWidth, SelfViewHeight - 49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = false;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
    
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH ,  667 * HEIGHT / 3  )];
    self.tableView.tableHeaderView = self.headView;
    
    
    [_tableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [_tableView registerClass:[RMZQTableViewCell class] forCellReuseIdentifier:@"reuse1"];
    [_tableView registerClass:[QDTJTableViewCell class] forCellReuseIdentifier:@"reuse2"];
    [_tableView registerClass:[JRTJTableViewCell class] forCellReuseIdentifier:@"reuse3"];
    [_tableView registerClass:[FourTableViewCell class] forCellReuseIdentifier:@"reuse4"];
    [self handledata];
    
    
}

- (void)arrayWithDictionary
{
    self.lightComicArr = [NSMutableArray array];
    self.bannerArr = [NSMutableArray array];
    self.bannerArray = [NSMutableArray array];
    self.dictionary =[NSMutableDictionary dictionary];
    self.mangeArr = [NSMutableArray array];
    self.comiclist1 = [NSMutableArray array];
    self.comiclist2 = [NSMutableArray array];
    self.comiclist3 = [NSMutableArray array];
    self.comiclist4 = [NSMutableArray array];
    self.comiclist5 = [NSMutableArray array];
    self.comiclist6 = [NSMutableArray array];
    self.comiclist7 = [NSMutableArray array];
    self.comiclist8 = [NSMutableArray array];
    self.comiclist9 = [NSMutableArray array];
    self.limitFreeArr = [NSMutableArray array];
    self.guessArr = [NSMutableArray array];
    self.bannerDic = [NSMutableDictionary dictionary];
    self.idDic = [NSMutableDictionary dictionary];
    self.keyArr = [NSMutableDictionary dictionary];
}



- (void)handledata
{
    [self arrayWithDictionary];
    
   [NetHandler dataWithUrl:@"http://mobilev3.ac.qq.com/Home/homePageDetailForIosV2/uin/0/local_version/3.1.0/channel/1001/guest_id/491A80FA-091B-4330-B939-7DE39BB24F7F" complant:^(id data) {
      
       _dictionary = [data objectForKey:@"data"];
       
       for (NSString *key in _dictionary) {
           
           [self.idDic setValue:[[_dictionary objectForKey:key] objectForKey:@"comic_list_id"] forKey:key];
           
       }
       
       _bannerDic = [_dictionary objectForKey:@"banner"];
       NSArray *array = [_bannerDic objectForKey:@"data"];
      
       for (NSMutableDictionary *dic in array) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [self.bannerArr addObject:recommend.banner_url];
           [self.bannerArray addObject:recommend];
          
           
       }
      
       
       NSMutableDictionary *mangaDic = [_dictionary objectForKey:@"manga"];
       NSArray *arr = [mangaDic objectForKey:@"data"];
       for (NSMutableDictionary *dic in arr) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_mangeArr addObject:recommend];
          
       }
       NSMutableDictionary *comicList1Dic = [_dictionary objectForKey:@"comicList1"];
       NSArray *arr1 = [comicList1Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr1) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist1 addObject:recommend];
          
           
           
           
         
       }
       NSMutableDictionary *comicList2Dic = [_dictionary objectForKey:@"comicList2"];
       NSArray *arr2 = [comicList2Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr2) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist2 addObject:recommend];
                }
       NSMutableDictionary *comicList3Dic = [_dictionary objectForKey:@"comicList3"];
       NSArray *arr3 = [comicList3Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr3) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist3 addObject:recommend];
          
       }
       NSMutableDictionary *guessDic = [_dictionary objectForKey:@"guessYouLike"];
       NSArray *arr4 = [guessDic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr4) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_guessArr addObject:recommend];
          
       }
       NSMutableDictionary *comicList4Dic = [_dictionary objectForKey:@"comicList4"];
       NSArray *arr5 = [comicList4Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr5) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist4 addObject:recommend];
         
       }
       NSMutableDictionary *comicList5Dic = [_dictionary objectForKey:@"comicList5"];
       NSArray *arr6 = [comicList5Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr6) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist5 addObject:recommend];
           
       }
       NSMutableDictionary *comicList6Dic = [_dictionary objectForKey:@"comicList6"];
       NSArray *arr7 = [comicList6Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr7) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist6 addObject:recommend];
          
       }
       NSMutableDictionary *comicList7Dic = [_dictionary objectForKey:@"comicList7"];
       NSArray *arr8 = [comicList7Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr8) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist7 addObject:recommend];
          
       }
       NSMutableDictionary *comicList8Dic = [_dictionary objectForKey:@"comicList8"];
       NSArray *arr9 = [comicList8Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr9) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist8 addObject:recommend];
           
       }
       NSMutableDictionary *comicList9Dic = [_dictionary objectForKey:@"comicList9"];
       NSArray *arr10 = [comicList9Dic objectForKey:@"data"];
       for (NSMutableDictionary * dic in arr10) {
           Recommend1 *recommend = [[Recommend1 alloc] init];
           [recommend setValuesForKeysWithDictionary:dic];
           [_comiclist9 addObject:recommend];
           
        
       }
       
       [self lubotu];
       [_tableView reloadData];
       
       
       
   }];


}
- (void)lubotu
{
    NSMutableArray *lunarr = [NSMutableArray arrayWithArray:_bannerArr];
    [LunBoTu shareCollection].time = 2;
    [LunBoTu shareCollection].delegate = self;
 
    [[LunBoTu shareCollection] imageWithUrlArray:lunarr frame:CGRectMake(0, 64, SelfViewWidth, (SelfViewHeight - 64 -49) / 3)];
    [self.headView addSubview:[[LunBoTu shareCollection] collectionView]];
    [self.headView addSubview:[[LunBoTu shareCollection] pageControl]];
    
    
}

- (void)didSelectCollectionView:(UICollectionView *)collectionView atItemImdexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.item);
    
    Recommend1 *rec = [self.bannerArray objectAtIndex:(indexPath.item )];

    if (rec.banner_type == 3) {
          LBTViewController *lbtvVC = [[LBTViewController alloc] init];
        
    
    lbtvVC.URl = rec.special_event_url;
  [self.navigationController pushViewController:lbtvVC animated:NO];
    }
    if (rec.banner_type == 1) {
    CartoonDetailsViewController *DetailsVC =[[CartoonDetailsViewController alloc] init];
        DetailsVC.idtext = rec.comic_id;
        [self.navigationController pushViewController:DetailsVC animated:NO];
        }
    
    
    

}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
    return 13;


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return 1;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section == 0) {
     
    RMZQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
        cell.titleLabel.text = @"日漫专区";
        cell.dataArray = _mangeArr;
        [self.keyArr setValue:@"manga" forKey:@"0"];
        cell.button.tag = 1000 + indexPath.row;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.delegate = self;
        
    [cell.collectionView reloadData];
    return cell;
        
        
    }
    if (indexPath.section == 1) {
        
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];

    NSMutableDictionary *lightComicDic = [_dictionary objectForKey:@"lightComic"];
    NSArray *arr = [lightComicDic objectForKey:@"data"];
    for (NSMutableDictionary *dic in arr) {
        Recommend1 *recommend = [[Recommend1 alloc] init];
        [recommend setValuesForKeysWithDictionary:dic];
        
    cell.titleLabel.text = [lightComicDic objectForKey:@"title"];
        self.more_event_url = [lightComicDic objectForKey:@"more_event_url"];
    [cell.bigImage sd_setImageWithURL:[NSURL URLWithString:recommend.img_url]placeholderImage:[UIImage imageNamed:@"5"]];
    }
    return cell;
    }
    if (indexPath.section == 2) {
        
        
    QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse2"];
    cell.titleLabel.text = @"强档推荐";
    cell.dataArray = _comiclist1;
    [cell.collectionView reloadData];
        cell.button.tag =1000+ indexPath.section;
        cell.delegate = self;
        [self.keyArr setValue:@"comicList1" forKey:@"2"];
        [cell.button setTitle:@">" forState:UIControlStateNormal];
    [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
        
    }
    if (indexPath.section == 3) {
    JRTJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse3"];
    NSMutableDictionary *limitFreeDic = [_dictionary objectForKey:@"limitFree"];
    NSDictionary *d = [limitFreeDic objectForKey:@"data"];
        
      
        Recommend1 *recommend = [[Recommend1 alloc] init];
        [recommend setValuesForKeysWithDictionary:d];
        
        
        cell.titleLabel.text = @"今日推荐";
        [cell.bigImage sd_setImageWithURL:[NSURL URLWithString:recommend.cover_url]placeholderImage:[UIImage imageNamed:@"5"]];
        cell.nameLabel.text = recommend.title;
        cell.artistLabel.text = recommend.artist_name;
        cell.briefLabel.text = recommend.brief_intrd;
        cell.briefLabel.numberOfLines = 0;
        
        return cell;
    }
    if (indexPath.section == 4) {
        QDTJTableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:@"reuse2"];
        cell.titleLabel.text = @"人气新作";
        cell.delegate =self;
        cell.dataArray = _comiclist2;
        cell.button.tag = 1000+indexPath.section;
        [self.keyArr setValue:@"comicList2" forKey:@"4"];

        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [cell.collectionView reloadData];
        return cell;

    }
    if (indexPath.section == 5) {
        QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse2"];
        cell.titleLabel.text = @"今日更新";
        cell.dataArray = _comiclist3;
        [self.keyArr setValue:@"comicList3" forKey:@"5"];
        cell.delegate = self;
        cell.button.tag = 1000+indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.collectionView reloadData];
        return cell;
    
    }
    if (indexPath.section == 6) {
        QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse2"];
        cell.titleLabel.text = @"猜你喜欢";
        cell.dataArray = _guessArr;
        cell.delegate =self;
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 7) {
        QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"少年";
        cell.dataArray = _comiclist4;
        [self.keyArr setValue:@"comicList4" forKey:@"7"];
        cell.delegate =self;
        cell.button.tag =1000+ indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 8) {
        QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"少女";
        cell.dataArray = _comiclist5;
        [self.keyArr setValue:@"comicList5" forKey:@"8"];
        cell.delegate =self;
        cell.button.tag = 1000+indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 9) {
        QDTJTableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"恐怖";
        cell.dataArray = _comiclist6;
        [self.keyArr setValue:@"comicList6" forKey:@"9"];
        cell.delegate = self;
        cell.button.tag = 1000 +indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 10) {
        QDTJTableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"魔幻";
        cell.dataArray = _comiclist7;
        cell.delegate = self;
        [self.keyArr setValue:@"comicList7" forKey:@"10"];
        cell.button.tag =1000 + indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 11) {
        QDTJTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"搞笑";
        cell.dataArray = _comiclist8;
        [self.keyArr setValue:@"comicList8" forKey:@"11"];
        cell.delegate = self;
        cell.button.tag =1000+ indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    if (indexPath.section == 12) {
        QDTJTableViewCell *cell = [tableView
            dequeueReusableCellWithIdentifier:@"reuse4"];
        cell.titleLabel.text = @"热血";
        cell.dataArray = _comiclist9;
        [self.keyArr setValue:@"comicList9" forKey:@"12"];
        cell.delegate = self;
        cell.button.tag =1000+ indexPath.section;
        [cell.button setTitle:@">" forState:UIControlStateNormal];
        [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        [cell.collectionView reloadData];
        return cell;
        
    }
    

    
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    

    return cell;
    
}

- (void)pushRecommend:(Recommend1 *)recommend
{

    CartoonDetailsViewController *DetailTV = [[CartoonDetailsViewController alloc] init];
   

    DetailTV.idtext = recommend.comic_id;
    [self.navigationController pushViewController:DetailTV animated:NO];

}


- (void)pushTheme:(Recommend1 *)theme
{

    CartoonDetailsViewController *DetailTV = [[CartoonDetailsViewController alloc] init];
    DetailTV.idtext = theme.comic_id;
   
    
    [self.navigationController pushViewController:DetailTV animated:NO];

}


- (void)push2Recommend:(Recommend1 *)recommend
{

    CartoonDetailsViewController *DetailTV = [[CartoonDetailsViewController alloc] init];
    DetailTV.idtext = recommend.comic_id;
    [self.navigationController pushViewController:DetailTV animated:NO];
}
- (void)buttonAction:(UIButton *)btn
{
    MoreViewController *moreVC = [[MoreViewController alloc] init];
    

   NSString *str = [_keyArr objectForKey:[NSString stringWithFormat:@"%ld",btn.tag - 1000]];
    NSString *str1 = [_idDic objectForKey:str];
    moreVC.textID = str1;
    
    [self.navigationController pushViewController:moreVC animated:NO];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 280 * HEIGHT;
    }else if(indexPath.section == 1)
    
    {
        return 170 *HEIGHT;
    
    }else if(indexPath.section == 2 || indexPath.section == 4 || indexPath.section == 5 || indexPath.section == 6)
    {
        return 460 * HEIGHT;
        
    }else if(indexPath.section == 3)
    {

    return 190 * HEIGHT;
    }else
    {
        return 490 /3 * 2 *HEIGHT;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section ==1) {
        
    
    TMMRViewController *tmmrVC = [[TMMRViewController alloc] init];
        tmmrVC.URl = self.more_event_url;
        [self.navigationController pushViewController:tmmrVC animated:NO];
      
    }else if (indexPath.section == 3){
        CartoonDetailsViewController *DetailsVC = [[CartoonDetailsViewController alloc] init];

        NSMutableDictionary *limitFreeDic = [_dictionary objectForKey:@"limitFree"];
        NSDictionary *d = [limitFreeDic objectForKey:@"data"];
        
        
        Recommend1 *recommend = [[Recommend1 alloc] init];
        [recommend setValuesForKeysWithDictionary:d];
        
        

        DetailsVC.idtext = recommend.comic_id;
        [self.navigationController pushViewController:DetailsVC animated:NO];
    }

}
-(void)barButtonAction1:(UIButton *)buth

{
    SearchViewController *search = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    
    
    NSLog(@"搜索");
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
